package com.pilot.game.service;

import com.pilot.game.domain.Favorite;
import com.pilot.game.domain.Plan;
import com.pilot.game.repository.FavoriteRepository;
import com.pilot.game.repository.PlanRepository;
import com.pilot.game.service.dto.FavoriteDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * Service Implementation for managing Favorite.
 */
@Service
@Transactional
public class FavoriteService {

    private final Logger log = LoggerFactory.getLogger(FavoriteService.class);

    private final FavoriteRepository favoriteRepository;
    private final PlanRepository planRepository;

    public FavoriteService(FavoriteRepository favoriteRepository, PlanRepository planRepository) {
        this.favoriteRepository = favoriteRepository;
        this.planRepository = planRepository;
    }

    /**
     * Save a favorite.
     *
     * @param favorite the entity to save
     * @return the persisted entity
     */
    public Favorite save(Favorite favorite) {
        log.debug("Request to save Favorite : {}", favorite);
        return favoriteRepository.save(favorite);
    }

    /**
     *  Get all the favorites.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<Favorite> findAll(Pageable pageable) {
        log.debug("Request to get all Favorites");
        return favoriteRepository.findAll(pageable);
    }

    /**
     *  Get one favorite by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true)
    public Favorite findOne(Long id) {
        log.debug("Request to get Favorite : {}", id);
        return favoriteRepository.findOne(id);
    }

    /**
     *  Delete the  favorite by id.
     *
     *  @param favoritePlan the id of the entity
     */
    public void delete(String favoritePlan) {
        log.debug("Request to delete Favorite : {}", favoritePlan);
        favoriteRepository.deleteByFavoritePlan(favoritePlan);
    }

    public List<Favorite> findByCustomerNameAndFavoritePlan(String customerName, String favoritePlan) {
        log.debug("Request to findByCustomerNameAndfavoritePlan Favorite : {},{}", customerName, favoritePlan);
        return favoriteRepository.findByCustomerNameAndFavoritePlan(customerName, favoritePlan);
    }

    public List<FavoriteDTO> findAllWithPlan(Example<Favorite> favoriteDTOExample) {
        log.debug("Request to get all Favorites with plan");
        List<FavoriteDTO> favoriteDTOList = new ArrayList<>();
        List<Favorite> repositoryAll = favoriteRepository.findAll(favoriteDTOExample);
        for (Favorite favorite : repositoryAll) {
            FavoriteDTO favoriteDTO = new FavoriteDTO(favorite);
            Plan one = planRepository.findOne(Long.parseLong(favorite.getFavoritePlan()));
            favoriteDTO.setPlan(one);
            favoriteDTOList.add(favoriteDTO);
        }
        return favoriteDTOList;
    }
}
